package com.sun.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author SunBingRui
 * @Date 2020/5/1 22:41
 */

public class MybatisUtils {
    //使用mybatis第一步：获取sqlSessionFactory对象

   private static SqlSessionFactory sqlSessionFactory;

    static {
        try {

            String configFile = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(configFile);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //有了sqlSessionFactory-->就可以获取sqlSession实例，
    //SqlSession完全包含了数据库执行SQL的所有方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }

}
